package com.project.weather.common;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 8765324882181340344L;
    private int defaultLength = 10;
    private int page;
    private int limit;
    private long start;
    private String orderName;
    private String orderType;
    private String jsonParam;
    private long recordsTotal;
    private long recordsFiltered;
    private T bean;
    private List<T> data;

    public PageBean() {
    }

    public PageBean<T> init(PageBean<T> pb, Class<T> _class) {
        T bean = null;
        if (this.jsonParam != null && !"".equals(this.jsonParam)) {
            ObjectMapper objectMapper = new ObjectMapper();

            try {
                bean = objectMapper.readValue(this.jsonParam, _class);
            } catch (JsonParseException var26) {
                var26.printStackTrace();
            } catch (JsonMappingException var27) {
                var27.printStackTrace();
            } catch (IOException var28) {
                var28.printStackTrace();
            } finally {
                if (bean == null) {
                    try {
                        bean = _class.newInstance();
                    } catch (InstantiationException var24) {
                        var24.printStackTrace();
                    } catch (IllegalAccessException var25) {
                        var25.printStackTrace();
                    }
                }

            }
        } else {
            try {
                bean = _class.newInstance();
            } catch (InstantiationException var29) {
                var29.printStackTrace();
            } catch (IllegalAccessException var30) {
                var30.printStackTrace();
            }
        }

        pb.setBean(bean);

        this.setStart((this.page-1)*this.limit);

        return pb;
    }

    public void clear() {
        this.setOrderName((String)null);
        this.setOrderType((String)null);
        this.setJsonParam((String)null);
        this.setBean((T) null);
    }

    public T getBean() {
        return this.bean;
    }

    public void setBean(T bean) {
        this.bean = bean;
    }

    public int getDefaultLength() {
        return this.defaultLength;
    }

    public void setDefaultLength(int defaultLength) {
        this.defaultLength = defaultLength;
    }

    public String getOrderName() {
        return this.orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public String getOrderType() {
        return this.orderType;
    }

    public void setOrderType(String orderType) {
        if (orderType != null && !"".equals(orderType)) {
            if (!"asc".equalsIgnoreCase(orderType) && !"desc".equalsIgnoreCase(orderType)) {
                this.orderType = "asc";
            } else {
                this.orderType = orderType;
            }
        } else {
            this.orderType = "asc";
        }

    }

    public String getJsonParam() {
        return this.jsonParam;
    }

    public void setJsonParam(String jsonParam) {
        this.jsonParam = jsonParam;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public List<T> getData() {
        return this.data;
    }

    public void setData(List<T> data) {
        this.data = (List)(data != null && data.size() != 0 ? data : new ArrayList());
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page == 0 ? this.defaultLength : page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public long getStart() {
        return this.start;
    }

    public long getRecordsTotal() {
        return this.recordsTotal;
    }

    public void setRecordsTotal(long recordsTotal) {
        this.recordsTotal = recordsTotal;
        this.recordsFiltered = recordsTotal;
    }

    public long getRecordsFiltered() {
        return this.recordsFiltered;
    }

    public void setRecordsFiltered(long recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }

    public String toString() {
        return "PageBean [draw=" + this.page + ", length=" + this.limit + ", start=" + this.start + ", orderName=" + this.orderName + ", orderType=" + this.orderType + ", jsonParam=" + this.jsonParam + ", recordsTotal=" + this.recordsTotal + ", recordsFiltered=" + this.recordsFiltered + ", pojo=" + this.bean + ", data=" + this.data + "]";
    }
}